package com.last1mile.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import com.last1mile.dao.interfaces.CityDao;
import com.last1mile.dao.interfaces.DistrictDao;
import com.last1mile.dao.interfaces.StateDao;
import com.last1mile.model.City;
import com.last1mile.model.District;
import com.last1mile.model.State;
import com.last1mile.pojo.CityDO;
import com.last1mile.pojo.DistrictDO;
import com.last1mile.pojo.StateDO;

@Component("areaServiceImpl")
@RemoteProxy(name = "areaServe")
public class AreaServiceImpl implements ApplicationContextAware {

    private static ApplicationContext context;

    /**
     * 查询中国的所属省州
     * 
     * @return
     */
    @RemoteMethod
    public List<State> queryStatesByCountryId(String coutryId) {
        List<State> stateList = new ArrayList<State>();
        try {
            StateDao stateDao = (StateDao) context.getBean("stateDao");
            List<StateDO> stateDoList = stateDao.selectByStateUpId(coutryId);
            if (!CollectionUtils.isEmpty(stateDoList)) {
                for (StateDO stateDo : stateDoList) {
                    stateList.add(new State(stateDo.getStateId(), stateDo.getCnName()));
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return stateList;
    }

    /**
     * 根据省州ID查询所属城市
     * 
     * @param stateId
     * @return
     */
    @RemoteMethod
    public List<City> queryCitysByStateId(String stateId) {
        List<City> cityList = new ArrayList<City>();
        try {
            CityDao cityDao = (CityDao) context.getBean("cityDao");
            List<CityDO> cityDoList = cityDao.selectByCityUpId(stateId);
            if (!CollectionUtils.isEmpty(cityDoList)) {
                for (CityDO cityDo : cityDoList) {
                    cityList.add(new City(cityDo.getCityId(), cityDo.getCnName()));
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return cityList;
    }

    /**
     * 根据城市ID查询所属区
     * 
     * @param cityId
     * @return
     */
    @RemoteMethod
    public List<District> queryDistrictsByCityId(String cityId) {
        List<District> districtList = new ArrayList<District>();
        try {
            DistrictDao districtDao = (DistrictDao) context.getBean("districtDao");
            List<DistrictDO> districtDoList = districtDao.selectByDistrictUpId(cityId);
            if (!CollectionUtils.isEmpty(districtDoList)) {
                for (DistrictDO districtDo : districtDoList) {
                    districtList.add(new District(districtDo.getDistrictId(), districtDo.getCnName()));
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return districtList;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }

}
